using System.Windows.Forms;


namespace OhioState.Controls
{
    /// <summary>
    /// Enumeration of possible axes to rotate along.
    /// </summary>
	public enum AXIS { X_AXIS, Y_AXIS, Z_AXIS };
    public class RotateAxisDragControl : TransformMouseController
	{
		private AXIS _axis;

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="targetControl">The control to which we should subscribe our
        /// mouse events.</param>
        /// <param name="axis">The axis to rotate along. Note, this is not the rotation axis
        /// but the more intuitive axis to which rotation occurs along.</param>
		public RotateAxisDragControl( AXIS axis ) : base()
		{
			//
			// The cameraCtrl controls need to be attached to the mouse handler's, but we will delay
			//    that until after a mouse down. The Examiner functions below determine what to 
			//    attach on a mouse down event. We will create two separate functions and attach
			//    one to each panel.
			//
			_axis = axis;
		}
        protected override void SetOwnerControl( Control control )
        {
            if (OwnerControl != null)
            {
                OwnerControl.MouseDown -= new MouseEventHandler(LeftRotate);
            }
            base.SetOwnerControl(control);
            if (OwnerControl != null)
            {
                OwnerControl.MouseDown += new MouseEventHandler(LeftRotate);
            }
        }

		#region Mouse Callbacks
		protected void LeftRotate(object sender, System.Windows.Forms.MouseEventArgs e) 
		{
			//
			// Check to see which mouse button was pressed.
			// A bit-wise and-mask is used for this.
			//
			if( (e.Button & MouseButtons.Left) == MouseButtons.Left )
			{
				if( this.NowInteracting == false )
				{
					Control currentCtrl = (Control) sender;
					//
					// Capture the Mouse movements, even those outside of this window.
					// This statement ensures that all mouse movement is associated with this
					//   process until the mouse button is released.
					//
					currentCtrl.Capture = true;
					// Change the cursor
					currentCtrl.Cursor = Cursors.WaitCursor;
					//
					// Add the Mouse Moves event handlers and the mouse up event handler.
					//
					switch (_axis) 
					{
						case AXIS.X_AXIS:
							m_MouseMoveDelegate = new MouseEventHandler( transformCtrl.RotateXAxis );
							break;
						case AXIS.Y_AXIS:
							m_MouseMoveDelegate = new MouseEventHandler( transformCtrl.RotateYAxis );
							break;
					}
					currentCtrl.MouseMove += m_MouseMoveDelegate;
					m_MouseUpDelegate = new MouseEventHandler( My_MouseUp );
					currentCtrl.MouseUp += m_MouseUpDelegate;
					//
					// Record this mouse position. 
					//
					transformCtrl.SetMousePosition( e.X, e.Y );
					BeginManipulation();

				}
			}
		}
		#endregion Mouse Callbacks
	}
}
